package com.medic.client.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.medic.entity.User;
import com.medic.entity.Weichat;
import com.medic.entity.group.pain.Medic;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.util.CacheUtil;
import com.medic.util.LoggerUtil;
import com.medic.util.StringUtil;

@RequestMapping("client/group")
@Controller
@SessionAttributes({ "weichat" })
public class GroupClientController {

	@Resource
	private GroupServiceImpl groupService;

	@RequestMapping("in")
	public String in(Model model, HttpSession session) {
		Weichat weichat = (Weichat) session.getAttribute("weichat");

		try {
			switch (weichat.getRole()) {
			case "user":
				model.addAttribute("groups", groupService.getNotinGroupByRole(weichat.getRoleid(), "user"));
				break;
			case "pharmacist":
				model.addAttribute("groups", groupService.getNotinGroupByRole(weichat.getRoleid(), "pharmacist"));
				break;
			case "assitant":
				model.addAttribute("groups", groupService.getNotinGroupByRole(weichat.getRoleid(), "assitant"));
				break;
			case "doctor":
				model.addAttribute("groups", groupService.getNotinGroupByRole(weichat.getRoleid(), "doctor"));
				break;
			}
		} catch (Exception ee) {
			ee.printStackTrace();
		}
		return "/client/group_in";
	}

	@RequestMapping("exist")
	public String exist(Model model, HttpSession session) {
		Weichat weichat = (Weichat) session.getAttribute("weichat");

		try {
			switch (weichat.getRole()) {
			case "user":
				model.addAttribute("groups", groupService.getGroupByUser(weichat.getRoleid()));
				break;
			case "pharmacist":
				model.addAttribute("groups", groupService.getGroupByUser(weichat.getRoleid()));
				break;
			case "assitant":
				model.addAttribute("groups", groupService.getGroupByUser(weichat.getRoleid()));
				break;
			case "doctor":
				model.addAttribute("groups", groupService.getGroupByUser(weichat.getRoleid()));
				break;
			}
		} catch (Exception ee) {
			ee.printStackTrace();
		}
		return "/client/group_exist";
	}

	@ResponseBody
	@RequestMapping("exist/{groupid}")
	public String existGroup(@PathVariable("groupid") int group, HttpSession session) {
		Weichat weichat = (Weichat) session.getAttribute("weichat");
		if (group == 0)
			return "{\"status\":\"error\"}";

		try {
			switch (weichat.getRole()) {
			case "user":
				groupService.userExitGroup(group, new int[] { weichat.getRoleid() });
				break;
			case "pharmacist":
				groupService.pharmacistExitGroup(group, new int[] { weichat.getRoleid() });
				break;
			case "assitant":
				groupService.assitantExitGroup(group, new int[] { weichat.getRoleid() });
				break;
			case "doctor":
				groupService.doctorExitGroup(group, new int[] { weichat.getRoleid() });
				break;

			}
		} catch (Exception ee) {
			ee.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{}";
	}

	@ResponseBody
	@RequestMapping("in/{groupid}")
	public String inGroup(@PathVariable("groupid") int group, HttpSession session) {
		LoggerUtil.debug(group + "");
		Weichat weichat = (Weichat) session.getAttribute(StringUtil.WECHAT_KEY);
		if (weichat == null) {
			LoggerUtil.debug("nulllllllllllllllllllllllllll");
		} else {
			LoggerUtil.debug(weichat.toString());
		}
		if (weichat.getRole().equals(StringUtil.USER_KEY)) {
			User user = (User) session.getAttribute(StringUtil.USER_KEY);
			for (Map.Entry<Integer, Medic> entry : CacheUtil.getMedics().entrySet()) {
				Medic medic = entry.getValue();
				if (medic.getSikerId() == user.getId() && medic.getMedicName().equals(StringUtil.MEDIC_NAME)) {
					if (medic.getAlert().size() <= 0) {
						return "{\"status\":\"no_medic\",\"medicId\":\"" + medic.getMedicId() + "\"}";
					}
				}
			}
		}
		if (group == 0)
			return "{\"status\":\"error\"}";

		try {
			switch (weichat.getRole()) {
			case "user":
				groupService.userJoinGroup(group, weichat.getRoleid());
				break;
			case "pharmacist":
				groupService.pharmacistJoinGroup(group, weichat.getRoleid());
				break;
			case "doctor":
				groupService.doctorJoinGroup(group, weichat.getRoleid());
				break;

			case "assitant":
				groupService.assitantJoinGroup(group, weichat.getRoleid());
				break;

			}
		} catch (Exception ee) {
			ee.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{}";
	}

	@ResponseBody
	@RequestMapping("detail/{groupid}")
	public Object detail(@PathVariable("groupid") int groupid) {
		Map<String, Object> object = new HashMap<String, Object>();
		try {

			object.put("group", groupService.getGroupById(groupid));

			object.put("memsnum", groupService.getUserCountByGroup(groupid));

			object.put("doctors", groupService.getDoctorByGroup(groupid));

			object.put("pharmacists", groupService.getPharmacistByGroup(groupid));

			object.put("assitants", groupService.getAssitantByGroup(groupid));

		} catch (Exception e) {
			e.printStackTrace();
			object.put("status", "error");
		}
		return object;
	}

	@RequestMapping("/enter")
	public void enter(@ModelAttribute(StringUtil.WECHAT_KEY) Weichat weichat) {
		System.out.println(weichat);
	}

}
